package com.ea.eadp.nexus.test.pageDefinition;

import com.ea.eadp.nexus.test.common.PageOperation;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

/**
 * Created by ChuanYe on 9/5/2014.
 */
public class ChallengeSetupPage extends BasePage {
    protected static final String phoneNumberInputBox = "phoneNumber";
    protected static final String cancelButton = "btnCancel";
    protected static final String invalidPhoneNumberErrorMessage = "Phone number is invalid";

    public ChallengeSetupPage(WebDriver driver) {
        super(driver, ChallengeSetupPage.class);
    }

    public void waitPageLoadCompleted() {
        WebDriverWait wait = new WebDriverWait(driver, 30, 1000);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(cancelButton)));
    }

    public void verifySmsInformation(String country, String phoneNumber) {
        WebElement elementRegionCode = driver.findElement(By.cssSelector("span[value=\""+country +"\"]"));
        Assert.assertNotNull(elementRegionCode, "The phone region code is wrong");
        WebElement elementPhoneNumber = driver.findElement(By.id(phoneNumberInputBox));
        Assert.assertNotNull(elementPhoneNumber, "not able to locate the phone number input element");
        Assert.assertEquals(elementPhoneNumber.getAttribute("value"),phoneNumber);
    }

    //send tfa code
    public void sendTFACode() {
        driver.findElement(By.cssSelector("#btnTFACreate > span > span")).click();
    }

    //App authenticator
    public void confirmUpdateToAppAuthenticator() {
        driver.findElement(By.id("btnTFAUpdate")).click();
    }
    public void submitPhoneSelection() {
        driver.findElement(By.id("btnAppSetup")).click();
    }

    public void cancel() {
        driver.findElement(By.id(cancelButton)).click();
    }

    //TFA code type selection
    public void setupTFAUsingEmail() {
        driver.findElement(By.xpath("//div[@id='panel-tfa-create']/div/div/div/div/div[1]/span")).click();
    }
    public void setupTFAUsingApp() {
        driver.findElements(By.className("origin-ux-radio-button-control")).get(1).click();
    }
    public void setupTFAUsingSms(String country, String phoneNumber) throws InterruptedException {
        driver.findElement(By.xpath("//div[@id='panel-tfa-create']/div/div/div/div[1]/div[5]/span")).click();
        WebDriverWait wait = new WebDriverWait(driver, 30, 1000);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("clientreg_country")));
        driver.findElement(By.xpath("//div[@id='clientreg_country']")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='clientreg_country']/div[2]/div")));

        String cssSelector= "a[value="+"\""+country+"\"] > span";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript("document.getElementById('country-selctrl').style.display='block';");
        Thread.sleep(1000);
        js.executeScript(stringBuilder.toString());
        sendInput(phoneNumberInputBox, phoneNumber);
    }

    //verify SQA
    public void setSecurityQuestion(String question) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(question);
        String cssSelector = "#clientreg_securityqa";
        PageOperation.SelectCssDropDownItem(driver, cssSelector, question);
    }
    public void setSecurityAnswer(String answer) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(answer);
        PageOperation.SendKeyToElementAfterWait(driver, "securityAnswer", answer);
    }
    public void submitSQA() {
        driver.findElement(By.cssSelector("#btnSQASubmit > span > span")).click();
    }

    //remember device
    public void checkRememberDevice() {
        WebElement element =  driver.findElement(By.id("label-trustThisDevice"));
        WebElement elementCheckBox = driver.findElement(By.id("trustThisDevice"));
        if(!elementCheckBox.isSelected()) {
            element.click();
        }
    }

    //verify TFA code
    public void setTFACode(String code) {
        sendInput("twofactorCode", code);
    }
    public void verifyTFACode(String codeType) {
        if (codeType.equalsIgnoreCase("APP")) {
            driver.findElement(By.id("btnAppVerify")).click();
        } else {
            driver.findElement(By.cssSelector("#btnTFAVerify > span > span")).click();
        }
    }

    public void verifyInvalidPhoneNumberErrorMessgeExist() {
        WebElement element = driver.findElement(By.cssSelector(".origin-ux-textbox-status-message"));
        Assert.assertNotNull(element, "not able to locate the invalid phone number error message");
        Assert.assertTrue(element.getText().contains(invalidPhoneNumberErrorMessage));
    }
}
